﻿using psafth.RCTiming.Common.Utilities;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace psafth.RCTiming.Main.Pages
{
    /// <summary>
    /// Interaction logic for UserPage.xaml
    /// </summary>
    public partial class UserPage : Page
    {
        private List<Driver> _availDrivers;

        public MainWindow MainWindow
        {
            get;
            set;
        }

        public DBContext DbContext
        {
            get
            {
                return MainWindow.DbContext;
            }
            set
            {
                MainWindow.DbContext = value;
            }
        }

        public UserPage(MainWindow m)
        {
            MainWindow = m;
            InitializeComponent();
            LoadAndBindDrivers();
        }

        private void LoadAndBindDrivers()
        {
            _availDrivers = DbContext.Drivers.OrderBy(d => d.FirstName).ToList();

            UserGrid.DataContext = _availDrivers;
        }

        private void LoadAndBindDrivers(string filter)
        {
            UserGrid.DataContext = _availDrivers.Where(d => d.FirstName.ToLower().Contains(filter.ToLower()) || d.LastName.ToLower().Contains(filter.ToLower()) || /* d.Club.FullName.ToLower().Contains(filter.ToLower()) || d.Club.ShortName.ToLower().Contains(filter.ToLower()) */ d.LicenceNumber.ToLower().Contains(filter.ToLower()));
        }

        private void UserGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (UserGrid.SelectedItem != null)
                {
                    if (UserGrid.SelectedItem is Driver)
                    {
                        Driver selectedDriver = (Driver)UserGrid.SelectedItem;

                        if (selectedDriver != null)
                        {
                            btnAddTransponder.Visibility = System.Windows.Visibility.Visible;

                            List<Transponder> transponders = DbContext.Transponders.Where(t => t.Driver.Id == selectedDriver.Id).ToList();
                            lbTransponders.DataContext = transponders;
                        }
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void tbFilter_TextChanged(object sender, TextChangedEventArgs e)
        {
            lbTransponders.DataContext = null;
            btnAddTransponder.Visibility = System.Windows.Visibility.Collapsed;
            btnRemoveTransponder.Visibility = System.Windows.Visibility.Collapsed;

            if (!string.IsNullOrWhiteSpace(tbFilter.Text))
            {
                LoadAndBindDrivers(tbFilter.Text);
            }
            else
            {
                LoadAndBindDrivers();
            }
        }

        private void btnAddTransponder_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (UserGrid.SelectedItem != null)
                {
                    if (UserGrid.SelectedItem is Driver)
                    {
                        Driver selectedDriver = (Driver)UserGrid.SelectedItem;

                        if (selectedDriver != null)
                        {

                            Dialogs.AddTransponderDialog prd = new Dialogs.AddTransponderDialog(selectedDriver, this);
                            prd.Owner = ParentWindow;
                            prd.ShowDialog();
                        }
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

        private Window ParentWindow
        {
            get
            {
                return Window.GetWindow(this);
            }
        }

        private void lbTransponders_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            btnRemoveTransponder.Visibility = System.Windows.Visibility.Collapsed;

            try
            {
                if (lbTransponders.SelectedItem != null)
                {
                    if (lbTransponders.SelectedItem is Transponder)
                    {
                        btnRemoveTransponder.Visibility = System.Windows.Visibility.Visible;
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void btnRemoveTransponder_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (lbTransponders.SelectedItem != null)
                {
                    if (lbTransponders.SelectedItem is Transponder)
                    {
                        int selectedTransponderId = ((Transponder)lbTransponders.SelectedItem).TransponderId;

                        Transponder selectedTransponder = DbContext.Transponders.FirstOrDefault(t => t.TransponderId == selectedTransponderId);

                        MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show(string.Format("Vill du ta bort transponder {0} från {1}?", selectedTransponder.TransponderId, selectedTransponder.Driver.FullName), "Bekräfta", System.Windows.MessageBoxButton.YesNo);
                        if (messageBoxResult == MessageBoxResult.Yes)
                        {
                            DbContext.Transponders.Remove(selectedTransponder);
                            DbContext.SaveChanges();

                            UpdateTransponderListBox();
                        }

                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

        internal void UpdateTransponderListBox()
        {
            try
            {
                if (UserGrid.SelectedItem != null)
                {
                    if (UserGrid.SelectedItem is Driver)
                    {
                        Driver selectedDriver = (Driver)UserGrid.SelectedItem;

                        if (selectedDriver != null)
                        {
                            btnAddTransponder.Visibility = System.Windows.Visibility.Visible;

                            List<Transponder> transponders = DbContext.Transponders.Where(t => t.Driver.Id == selectedDriver.Id).ToList();
                            lbTransponders.DataContext = transponders;

                        }
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void btnAddDriver_Click(object sender, RoutedEventArgs e)
        {
            Dialogs.AddDriverDialog prd = new Dialogs.AddDriverDialog(this);
            prd.Owner = ParentWindow;
            prd.ShowDialog();
        }

        public void UpdateUserDataGrid()
        {
            LoadAndBindDrivers();
        }

        private void btnRemoveDriver_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (UserGrid.SelectedItem != null)
                {
                    if (UserGrid.SelectedItem is Driver)
                    {
                        Driver selectedDriver = (Driver)UserGrid.SelectedItem;

                        if (selectedDriver != null)
                        {
                            MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show(string.Format("Vill du ta bort förare {0}?", selectedDriver.FullNameWithLicence), "Bekräfta", System.Windows.MessageBoxButton.YesNo);
                            if (messageBoxResult == MessageBoxResult.Yes)
                            {
                                DatabaseHelper.DoWithDB<DBContext>(db =>
                                {
                                    Driver driver = db.Drivers.First(d => d.Id == selectedDriver.Id);

                                    if (driver != null)
                                    {
                                        db.Drivers.Remove(driver);
                                        db.SaveChanges();
                                    }
                                });

                                LoadAndBindDrivers();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }
    }
}
